package com.estronger.shopmanage.adapter.tasking;

import android.Manifest;
import android.app.Dialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.support.v4.app.ActivityCompat;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.estronger.shopmanage.R;
import com.estronger.shopmanage.bean.InstallDPG;
import com.estronger.shopmanage.ui.install.pg.InstallAffirmCondActivity;
import com.estronger.shopmanage.ui.install.pg.InstallAffirmPGActivity;
import com.estronger.shopmanage.ui.install.pg.InstallPGActivity;
import com.estronger.shopmanage.ui.install.pg.InstallPGDetailActivity;
import com.estronger.shopmanage.utils.ConvertUtils;
import com.estronger.shopmanage.utils.PermissionUtils;
import com.estronger.shopmanage.utils.Toast;
import com.estronger.shopmanage.view.AppDialog;
import com.soonfor.evaluate.tools.CommonUtils;

import java.util.ArrayList;

import cn.jesse.nativelogger.NLogger;

import static com.estronger.shopmanage.R.id.ll_detail;

public class InstallDPGListAdapter extends BaseAdapter implements OnClickListener {

    InstallPGActivity mContext;
    public Dialog dialog;
    private int whitchToCall = 0;//是点击了那个按钮后跳出打电话对话框的
    ArrayList<InstallDPG.Datas.InstallDPGListData> mList;

    public InstallDPGListAdapter(InstallPGActivity mContext, ArrayList<InstallDPG.Datas.InstallDPGListData> datas) {
        this.mContext = mContext;
        this.mList = datas;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        if (mList != null)
            return mList.size();
        else
            return 0;
    }

    @Override
    public InstallDPG.Datas.InstallDPGListData getItem(int position) {
        // TODO Auto-generated method stub
        if (mList == null || mList.size() <= 0)
            return null;
        else
            return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        // TODO Auto-generated method stub
        final ListItemView ltv;
        if (view == null) {
            view = LayoutInflater.from(mContext).inflate(
                    R.layout.adpter_install_daipai, null);
            ltv = new ListItemView(view);
            view.setTag(ltv);
        } else {
            ltv = (ListItemView) view.getTag();
        }
        try {
            ltv.Installsname.setText(ConvertUtils.formatNullF(mList.get(position).cstbuildname) + ConvertUtils.formatNull(mList.get(position).cstname));
            ltv.Install_item_location.setText(ConvertUtils.formatNull(mList.get(position).address));
            ltv.Install_item_time.setText(mList.get(position).pidate);
            ltv._phone = mList.get(position).cstmobile;
            ltv.TitelName = ltv.Installsname.getText().toString();
            ltv.deliver_item_phoneno.setText(ConvertUtils.formatNull(mList.get(position).cstmobile));
        } catch (Exception e) {
            e.printStackTrace();
        }
        //去详情
        ltv.ll_detail.setTag(position);
        ltv.Install_phone_call.setTag(position);
        ltv.Install_send_sms.setTag(position);
        ltv.Install_cond_sure.setTag(position);
        ltv.Install_item_receive.setTag(position);

        ltv.ll_detail.setOnClickListener(this);
        ltv.Install_phone_call.setOnClickListener(this);
        ltv.Install_send_sms.setOnClickListener(this);
        ltv.Install_cond_sure.setOnClickListener(this);
        ltv.Install_item_receive.setOnClickListener(this);
        return view;
    }

    class ListItemView {
        public TextView Installsname, Install_item_location, Install_item_time, deliver_item_phoneno;
        public ImageView Install_phone_call, Install_send_sms;
        public LinearLayout ll_detail, Install_cond_sure, Install_item_receive;
        public String _phone;
        public String TitelName;

        public ListItemView(View view) {
            this.Installsname = (TextView) view.findViewById(R.id.installsname);
            this.Install_item_location = (TextView) view.findViewById(R.id.install_item_location);
            this.Install_item_time = (TextView) view.findViewById(R.id.install_item_time);
            this.Install_phone_call = (ImageView) view.findViewById(R.id.install_phone_call);
            this.Install_send_sms = (ImageView) view.findViewById(R.id.install_send_sms);
            this.ll_detail = (LinearLayout) view.findViewById(R.id.ll_detail);
            this.Install_cond_sure = (LinearLayout) view.findViewById(R.id.install_item_tiaojiancomfir);
            this.Install_item_receive = (LinearLayout) view.findViewById(R.id.install_item_receive);
            this.deliver_item_phoneno = (TextView) view.findViewById(R.id.deliver_item_phoneno);
        }
    }

    public String phone;

    @Override
    public void onClick(View v) {
        int position = (int) v.getTag();
        InstallDPG.Datas.InstallDPGListData itemData = getItem(position);
        String installId = itemData.dlvid;
        phone = itemData.cstmobile;
        String TitleName = "";
        if (TextUtils.isEmpty(itemData.cstbuildname)) {
            TitleName = itemData.cstname;
        } else {
            TitleName = itemData.cstbuildname + "-" + itemData.cstname;
        }
        Intent intent;
        switch (v.getId()) {
            case ll_detail:
                intent = new Intent(mContext, InstallPGDetailActivity.class);
                intent.putExtra("InstallItem", itemData);
                mContext.startActivity(intent);
                break;
            case R.id.install_phone_call:// 点击了测量条目上的电话小图标

                if (mContext.isRightNumber(phone)) {
                    dialog = AppDialog.createPgDialog(mContext,
                            "[" + TitleName + "]", phone, this, position);
                    whitchToCall = 0;
                    dialog.show();
                }
                break;
            case R.id.install_send_sms:// 点击了测量条目上的发短信小图标

                if (mContext.isRightNumber(phone)) {
                    Uri uri = Uri.parse("smsto:" + phone);
                    intent = new Intent(Intent.ACTION_SENDTO, uri);
                    mContext.startActivity(intent);
                }
                break;
            case R.id.install_item_tiaojiancomfir:
                if (mContext.isRightNumber(phone)) {
                    dialog = AppDialog.createPgDialog(mContext,
                            "[" + TitleName + "]", phone, this, position);
                    whitchToCall = 1;
                    dialog.show();
                }
                break;
            case R.id.install_item_receive:
                if (mContext.isRightNumber(phone)) {
                    dialog = AppDialog.createPgDialog(mContext,
                            "[" + TitleName + "]", phone, this, position);
                    whitchToCall = 2;
                    dialog.show();
                }
                break;
            case R.id.btnfSure:
                try {
                    if (PermissionUtils.isCallPhoneGranted(mContext)) {
                        intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
                                + phone));
                        if (ActivityCompat.checkSelfPermission(mContext, Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED) {
                            Toast.show(mContext, "您还没有开启拨打电话权限，请先开启！", Toast.LENGTH_SHORT);
                        }
                        if (whitchToCall == 1) {// 这里是判断拨打电话后是否需要跳转其它界面

                            int requestCode = Integer.valueOf(installId);
                            InstallPGActivity.CALL_RESULT_MSCONDITION = CommonUtils.getRequestCode(installId);;
                            InstallPGActivity.idpData = itemData;
                            mContext.startActivityForResult(intent,
                                    InstallPGActivity.CALL_RESULT_MSCONDITION);
                        } else if (whitchToCall == 2) {
                            InstallPGActivity.CALL_RESULT_PGSURE = CommonUtils.getRequestCode(installId);
                            InstallPGActivity.idpData = itemData;
                            mContext.startActivityForResult(intent,
                                    InstallPGActivity.CALL_RESULT_PGSURE);
                        } else {
                            mContext.startActivity(intent);
                        }
                    } else {
                        PermissionUtils.ToSetPermissions(mContext, R.string.PermissionsCallPhone);
                    }
                    dialog.dismiss();
                } catch (Exception e) {
                    NLogger.e("" + e.getMessage());
                }
                break;
            case R.id.btnfCancel:
                if (whitchToCall == 1) {// 这里是判断拨打电话后是否需要跳转其它界面
                    Intent intent1 = new Intent(mContext, InstallAffirmCondActivity.class);
                    intent1.putExtra("InstallItem", itemData);
                    mContext.startActivityForResult(intent1, InstallPGActivity.REFLESH_DATA);
                } else if (whitchToCall == 2) {
                    Intent intent2 = new Intent(mContext, InstallAffirmPGActivity.class);
                    intent2.putExtra("InstallItem", itemData);
                    mContext.startActivityForResult(intent2, InstallPGActivity.REFLESH_DATA);
                }
                dialog.dismiss();
                break;

        }
    }
}
